#!/bin/sh
# mkpro.sh - Make the project files for source code using VIM.

# 检测ctags和cscope的存在
if [ ! -e /usr/bin/ctags ]
then
    echo "ctags is not be found!"
    exit
fi

if [ ! -e /usr/bin/cscope ]
then
    echo "cscope is not be found!!"
    exit
fi

# 存放所有项目配置文件的总目录在~/jacob.pro下
pro_all=~/.jacob.pro
if [ ! -d ${pro_all} ]
then
    echo "Make a pro setting path for all at ~/.jacob.pro"
    mkdir ${pro_all}
else
    echo "Project setting path for all at ~/.jacob.pro"
fi
# 设置项目名称以及源码路径
echo "Pro Setting Init:"
echo -n "\tPro Name: "
read pro_name
# 如果该项目名已经存在，则询问是否覆盖
while [ -d ${pro_all}/${pro_name} ]
do
    echo "\t\tThe ${pro_name}.pro has been existing!!!!"
    echo -n 'Over Write?[no/yes]: '
    read over_write
    
    if [ "$over_write" = "yes" ]
    then
        break
    else
        echo -n "\tAnother Name: "
        read pro_name
    fi
done

if [ ! -d ${pro_all}/${pro_name} ]
then
    mkdir ${pro_all}/${pro_name}
fi
# 如果源码路径不存在，则要求重新输入源码路径
echo -n "\tSource Path: "
read pro_path
while [ ! -d ${pro_path} ]
do
    echo "\t\tPath is NOT Existing.Check it, please."
    echo -n "\tSource Path: "
    read pro_path
done

echo ""
echo "Pro Setting:"
echo "\tPro Name: ${pro_name}"
echo "\tSource Path: ${pro_path}"
echo ""
# 将项目信息写入对应的config文件
echo "pro.name=${pro_name}" >${pro_all}/${pro_name}/config
echo "pro.path=${pro_path}" >>${pro_all}/${pro_name}/config

echo "To make tags, waiting..."
echo "ctags -R --c-kinds=+p --fields=+S"
cd ${pro_path}
ctags -R --c-kinds=+p --fields=+S -f ${pro_all}/${pro_name}/tags
echo "cscope -Rbq"
cscope -Rbq
mv -f *.out ${pro_all}/${pro_name}/
echo "Done"

